export class Rule {
    private declare readonly sceneType: number;

    constructor(sceneType: number) {
        this.sceneType = sceneType;
    }

    /**
     * 跟踪过去的选择，确保不会连续选择同一种类型的东西3到5次
     * @private
     */
    private pastLists: number[] = [];
    /**
     * 当前的数组项下标 0:目标 1:干扰 2:数字 3:小鱼
     * @private
     */
    private currentIndex: number = 0;

    /**
     * 随机生成0-3的下标  0：目标，1：干扰，2：数字，3：鱼，
     * sceneType:1->0,1,2   2->0,1,2,3  3->0(顺序),1,2,3
     */
    generate(): number {
        const arraySize = this.sceneType == 1 ? 3 : 4;
        // 3或5
        const times = Math.floor(Math.random() * 3) + 3;
        let index;
        // 如果过去的选择中有3次-5次连续相同的数组，则改变索引
        if (this.pastLists.length >= times && this.pastLists.slice(-times).every(i => i === this.currentIndex)) {
            index = this.pastLists[times - 1] - 1;
        } else {
            index = Math.floor(Math.random() * arraySize);
        }
        this.currentIndex = index;
        this.pastLists.push(this.currentIndex);
        if (this.pastLists.length > 5) {
            this.pastLists.shift();
        }
        return this.currentIndex;
    }

    /**
     * 生成n个 0-99 的下标数，并返回这些下标数组，另返回一个数，该数只有80%的概率在这些下标数组中。
     */
    generateArrayAndTarget(n: number) {
        function randomNumber(count: number, min: number, max: number) {
            let numbers: number[] = [];
            while (numbers.length < count) {
                let num = Math.floor(Math.random() * (max - min)) + min;
                if (!numbers.includes(num)) {
                    numbers.push(num);
                }
            }
            return numbers;
        }

        let result: number[] = randomNumber(n, 1, 100);
        let random = Math.random();
        let inArray: boolean = random > 0.2;
        let target: number;
        if (inArray) {
            target = result[Math.floor(Math.random() * result.length)];
        } else {
            do {
                target = Math.floor(Math.random() * 100)
            } while (result.includes(target))
        }
        return {
            numbers: result,
            target: target,
            targetInArray: result.includes(target)
        }
    }
}